// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
}) // 使用当前云环境
const db = cloud.database();
const chapterContent = "contents";
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  let {
    id,
    uuid,
    content,
    type
  } = event;
  const _ = db.command;
  if(type=="set"){
    db.collection(chapterContent).add({
      data:{
        id,
        list:[{
          uuid,
          content:content
        }]
      }
    });
  }else{
    db.collection(chapterContent).where({
      id
    }).update({
      data: {
        list: _.push({
          uuid,
          content:content
        })
      }
    });
  }
  return {
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}